import re
import threading
import urllib.request

from bs4 import BeautifulSoup

from util import utils


TAG = ''

initUrl = 'http://f.p03.space/forumdisplay.php?fid=21'
indexUrl = 'http://f.p03.space/forumdisplay.php?fid=21&page='

orginNetUrl = 'http://f.p03.space/'

path = '我爱我妻/'


def saveImages(imglist, filePath):
    utils.mkdir(filePath)
    number = 1
    for imageURL in imglist:
        splitPath = imageURL.split('.')
        print(imageURL)
        fTail = splitPath.pop()
        if len(fTail) > 3:
            fTail = 'jpg'
        fileName = filePath + "/" + str(number) + "." + fTail
        # 对于每张图片地址，进行保存
        try:
            utils.downLoad(orginNetUrl + imageURL, fileName)
        except urllib.request.URLError as e:
            print(e.reason)
        number += 1


def getAllImg(html):
    imgre = re.compile(r'file=\"(.+?\.jpg)\"')
    imglist = imgre.findall(html)
    return imglist


def getTextInA(content):
    if str(content).__contains__("</font>"):
        res = r'>(.*?)</font>'
    else:
        res = r'>(.*?)</a>'

    s = re.findall(res, str(content), re.S | re.M)
    s = str(s).replace("\\n","").replace("\\r","")
    return s


def getUrlsFromPage(html):
    urlList={}
    url = ''
    name = ''
    t1 = BeautifulSoup(html, 'html.parser').find_all('a')

    for t2 in t1:
        s = str(getTextInA(t2))
        if str(t2.get('href')).__contains__('viewthread.php?'):
            if s.__contains__('<'):
                pass
            else:
                url = str(t2.get('href'))
                name = s
                urlList[url] = name

    print(urlList)
    return urlList


def getImgFormUrlList(urlList):
    for url in urlList:
        saveImages(getAllImg(utils.getHtmlUtf_8(orginNetUrl + url)), path + "/" + urlList[url] + '/')



def run(i):
    if i == 0:
        getImgFormUrlList(getUrlsFromPage(utils.getHtmlUtf_8(initUrl)))
    else:
        getImgFormUrlList(getUrlsFromPage(utils.getHtmlUtf_8(indexUrl + str(i))))
# print(saveImages(getAllImg(utils.getHtmlUtf_8(testImgListUrl)),path+"/"+utils.getTimeName()+'/'))

def loop11():
    run(n)

n = 10

if __name__ == "__main__":
    while 0 <= n < 100:
        n = n + 1
        t = threading.Thread(target=loop11, name='LoopThread1')
        t.start()
